package main

import "fmt"

type List struct {
	Head *Node
	Tail *Node
}

type Node struct {
	Prev  *Node
	Next  *Node
	Value int
}

func NewList() *List {
	return &List{Head: nil}
}

func (l *List) Append(node *Node) {
	if l.Head == nil {
		l.Head = node
		l.Tail = node
	} else {
		node.Prev = l.Tail
		l.Tail.Next = node
		l.Tail = node
	}
}

func (l *List) Remove(index int) {
	current := l.Head
	for i := 0; i < index; i++ {
		current = current.Next
	}
	current.Prev.Next = current.Next //current.Prev
}

func (l *List) Search(i int) bool {
	current := l.Head
	for current != nil {
		if current.Value == i {
			return true
		}
		current = current.Next
	}
	return false
}

func (l *List) MoveToFront(node *Node) {
	if node.Prev == nil {
		return
	} else if node.Next == nil {
		node.Prev.Next = nil
		node.Next = l.Head
		l.Head = node
	} else {
		node.Prev.Next = node.Next
		node.Next = l.Head
		l.Head = node
	}
}

func main() {
	l := NewList()
	l.Append(&Node{Value: 1})
	l.Append(&Node{Value: 2})
	node := &Node{Value: 3}
	l.Append(node)
	l.MoveToFront(node)
	//l.Remove(1)
	//fmt.Println(l.Search(2))
	fmt.Println(l)
}
